package com.yequan.leetcode;

import java.util.Scanner;
public class Main3 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n;
        while (true) {
            n = sc.nextInt();
            if (n > 1000) {
                System.out.println("n不能大于 1000,请重新输入");
            } else {
                break;
            }
        }
        int[] inputA = new int[n];
        for (int i = 0; i < n; i++) {
            inputA[i] = sc.nextInt();
        }
        int outputCount = sc.nextInt();
        int[] a = new int[n];
        // bloom去重
        int[] easyBloom = new int[1000];
        int k = 0;
        int size = 0;
        for (int i = 0; i < n; i++) {
            int bloomIndex = inputA[i] % 1000;
            if (easyBloom[bloomIndex] == 0) {
                size++;
                a[k++] = inputA[i];
                easyBloom[bloomIndex] = 1;
            }
        }

        //插入排序(从小到大排序),
        int scr = 0;
        for (int i = 0; i < n - 1; i++) {
            for (int j = i + 1; j < n; j++) {
                if (a[i] > a[j]) {
                    scr = a[i];
                    a[i] = a[j];
                    a[j] = scr;
                }
            }
        }
        //找出 topN and minN
        int left = n - size, right = n-1;
        boolean repeat = false;
        int targetSum = 0;
        int currentOutputCount = 1;
        for (int i = n - size; i < n; i++) {
            if (currentOutputCount > outputCount) {
                break;
            } else {
                currentOutputCount++;
            }
            // 检测是否重叠
            if (left == right) {
                repeat = true;
                break;
            }
            // 取值存下来
            targetSum += (a[left] + a[right]);
            left++;
            right--;
        }

        if (repeat) {
            System.out.println(-1);
        } else {
            System.out.println(targetSum);
        }
    }

}



